import Vue from 'vue'
import Router from 'vue-router'

import Login from '@/components/Login.vue'
import personalData from '@/components/personalData.vue'
import UserFaBu from '@/components/UserFaBu.vue'
import XiangQing from '@/components/XiangQing.vue'
import index from '@/components/index'
import MyDinDan from '@/components/MyDinDan'
import JieDan from '@/components/JieDan'
import UpData from '@/components/UpData'




Vue.use(Router)

var router = new Router({
    routes: [
        {
            path: '/UpData',
            component: UpData,
            name: 'UpData'
        },
        {
            path: '/JieDan',
            component: JieDan,
            name: 'JieDan'
        },
        {
            path: '/',
            component: Login,
            name: 'Lo'
        },
        {
            path: '/index',
            component: index,
            name: 'index'
        },
        {
            path: '/PD',
            component: personalData,
            name: 'PD'
        },
        {
            path: '/UF',
            component: UserFaBu,
            name: 'UF'
        },
        {
            path: '/XQ',
            component: XiangQing,
            name: 'XQ'
        },
        {
            path: '/MyDinDan',
            component: MyDinDan,
            name: 'MyDinDan'
        }

    ]
})


//挂载 路由守卫
router.beforeEach((to, from, next) => {
    //to 将要 访问的路径
    //from 代表从那个路径跳转而来的
    //next 是一个函数 表示放行
    // next()  放行 next("/login") 强制跳转
      //获取token
      const tokenStr =  localStorage.getItem("tokenlo")
     // console.log(tokenStr);
    if(to.path==='/'&&tokenStr)return next("/JieDan")
    //判断是不是登录页面
    if(to.path==='/')return next()
    if(to.path==='/index')return next()
   //判断是不是合格的token
    if (!tokenStr) return next('/')
   //有token就直接放行
   if (tokenStr) return  next()
   

 
 }) 


export default router